package com.diandireader.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.diandireader.domain.BookMarkVO;
import com.diandireader.util.FileHelper;
import com.diandireader.util.StringHelper;

public class DianDiReaderService {

	private SQLiteDatabase db;
	private static final String defaultDB = "diandireader.sqlite";
	private File file;
	public DianDiReaderService(){
	}
	
	private void openDb(){
		file = new File(FileHelper.getStrogeFolder()+"/"+FileHelper.diandiReaderDb+"/"+defaultDB);
		db = SQLiteDatabase.openOrCreateDatabase(file, null);
	}
	
	//保存上一次度到的位置
	public void saveLastReadPosition(String fileName,int position){
		ContentValues values = new ContentValues();
		values.put("name", fileName);
		values.put("position", position);
		db.insert("remember", null, values);
	}
	
	//读取上一次读到的位置
	public int readLastReadPosition(){
		int position = 0;
		return position;
	}
	//更新上一次读到的位置
	public void saveOrUpdateReadPosition(String fileName,int position){
		if(db==null||!db.isOpen()){
			openDb();
		}
		Cursor cursor = db.query("remember", new String[]{"id","position"}, "name= ?", new String[]{fileName}, null, null, null);
		//如果没有就创建
		if(cursor.getCount()==0){
			saveLastReadPosition(fileName,position);
		}else {
		//如果有了,就更新
			cursor.moveToFirst();
			int id = cursor.getInt(0);
			System.out.println(cursor.getInt(1));
			ContentValues values = new ContentValues();
			values.put("position", position);
			db.update("remember", values, "id=?", new String[]{id+""});
		}
		close();
	}
	
	//关闭DB
	public void close(){
		if(db.isOpen())
		db.close();
	}
	
	//operation about bookmark
	public void saveBookmark(String bookName,String bookMarkName,long skipBytes){
		if(db==null||!db.isOpen()){
			openDb();
		}
		//if this bookmarks exits
		Cursor cursor = db.query("bookmark", new String[]{"id"}, "name=? and position=?", new String[]{bookName,skipBytes+""}, null, null, null);
		if(cursor.getCount()>0)
			return;
		//if this bookmarks isnot exits
		ContentValues cv = new ContentValues();
		cv.put("name", bookName);
		cv.put("bookmarkname", bookMarkName==""?"未命名":bookMarkName);
		cv.put("position", skipBytes);
		cv.put("time",StringHelper.dateToString(null,null));
		db.insert("bookmark", null, cv);
		close();
	}
	
	//load bookmarks about this book
	public List<BookMarkVO> listBookmark(String bookName){
		List<BookMarkVO> list = new ArrayList();
		if(db==null||!db.isOpen()){
			openDb();
		}
		Cursor cursor = db.query("bookmark", new String[]{"*"}, "name=?", new String[]{bookName}, null, null, null);
		while(cursor.moveToNext()){
			BookMarkVO bm = new BookMarkVO();
			bm.setId(cursor.getInt(cursor.getColumnIndex("id")));
			bm.setBookmarkName(cursor.getString(cursor.getColumnIndex("bookmarkname")));
			bm.setSkipBytes(cursor.getInt(cursor.getColumnIndex("position")));
			bm.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(bm);
		}
		cursor.close();
		close();
		return list;
	}
	//delete bookmark about this book
	public void deleteBookmark(int id){
		if(db==null||!db.isOpen()){
			openDb();
		}
		db.delete("bookmark", "id=?", new String[]{id+""});
		close();
	}
}
